$(function(){
	
	//构建户籍选择列表
	createHouseholdSel();

});

//构建户籍选择列表
function createHouseholdSel()
{
	$.ajax({
        type : 'post',  
        url : '/pac/location/grade/next',
        data : {  
            'parentId' : '0'
        },
        success:function(data){
        	var locations=data;
        	if(undefined!=locations&&locations.length>0)
        	{
        		var myHousehold=$("#txtHousehold").val();
        		for(var n=0;n<locations.length;n++)
        		{
        			var location=locations[n];
        			var selected="";
        			if((null!=myHousehold&&""!=myHousehold&&undefined!=myHousehold)&&(myHousehold==location.name))
        			{
        				selected="selected='selected'";
        			}
        			else if("重庆市"==location.name)
        			{
        				selected="selected='selected'";
        			}
        			var selOpLocation="<option value='"+location.name+"' "+selected+">"+location.name+"</option>";
        			$("#selHousehold").append(selOpLocation);
        		}
        	}
        }
    });
}

//提交表单
function submitFrm()
{
	var txtName=$("#txtName").val();
	if(null==txtName||""==txtName)
	{
		alert("请填写姓名");
		return false;
	}
	
	var txtBirthday=$("#txtBirthday").val();
	if(null==txtBirthday||""==txtBirthday)
	{
		alert("请填写出生日期");
		return false;
	}
	
	var txtPhone=$("#txtPhone").val();
	if(null==txtPhone||""==txtPhone)
	{
		alert("请填写手机号");
		return false;
	}
	
	if(!checkMobile(txtPhone))
	{
		alert("请填写正确的手机号");
		return false;
	}
	
	var txtCaptcha=$("#txtCaptcha").val();
	if(null==txtCaptcha||""==txtCaptcha)
	{
		alert("请填写验证码");
		return false;
	}
	
	//检查微信验证码
	$.ajax({
        type : 'post',  
        url : '/pac/weixin/register/captcha/check',
        data : {  
            'phoneno' : txtPhone,
            'captcha':txtCaptcha
        },
        success:function(data){
        	if(data.result==true)
        	{
        		$("#frmAddUser").submit();
        		$(".ui-btn-corner-all").attr("onclick","");
        	}
        	else
        	{
        		alert(data.msg);
        	}
        }
    });
}

/**
 * 下发手机验证码
 */
function sendCaptcha()
{
	var txtPhone=$("#txtPhone").val();
	if(null==txtPhone||""==txtPhone)
	{
		alert("请填写手机号");
		return false;
	}
	
	if(!checkMobile(txtPhone))
	{
		alert("请填写正确的手机号");
		return false;
	}
	timerCaptchaRegister=setInterval("timerCaptcha()",1000);
	$.ajax({
        type : 'post',  
        url : '/pac/weixin/register/captcha/send',
        data : {  
            'phoneno' : txtPhone
        },
        success:function(data){
        	if(data.result==true)
        	{
        		var sendStrWait="<div id='spanTimerRegister' style='display:inline;'>60</div>秒后重新获取";
        		$("#btnSendCaptcha").html(sendStrWait);
        		$("#btnSendCaptcha").css("color","#AAAAAA");
        	}
        	else
        	{
        		alert(data.msg);
        	}
        }
    });
}

/**
 * 验证码定时器
 */
var timer=59;
function timerCaptcha()
{
	var sendStr="<div onclick='sendCaptcha()'>获取验证码</div>";
	if(timer>0)
	{
		timer=timer-1;
		$("#spanTimerRegister").text(timer);
	}
	else
	{
		clearInterval(timerCaptchaRegister);
		$("#btnSendCaptcha").html(sendStr);
		$("#btnSendCaptcha").css("color","blue");
	}
}

/**
 * 修改用户信息
 */
function submitEditFrm()
{
	var txtName=$("#txtName").val();
	if(null==txtName||""==txtName)
	{
		alert("请填写姓名");
		return false;
	}
	
	var txtBirthday=$("#txtBirthday").val();
	if(null==txtBirthday||""==txtBirthday)
	{
		alert("请填写出生日期");
		return false;
	}
	
	$("#frmEditUser").submit();
	$(".ui-btn-corner-all").attr("onclick","");
}

/**
 * 进入编辑用户信息界面
 */
function toEditUser()
{
	$("#divViewUser").hide();
	$("#divEditUser").show();
}
